package www.qiyue.bj;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.qiyue.utils.UUIDGenerator;

import java.util.*;

@RestController
public class Bj_contract {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("bj")
    public String test(){
        List<String> list = new ArrayList<>();
        Collections.addAll(list,"FC-20190628218");
        String feeSql="select COLUMN_NAME from information_schema.COLUMNS where table_name = 'ais_fee' and TABLE_SCHEMA='ais_bj'";
        List<Map<String, Object>> feeList = jdbcTemplate.queryForList(feeSql);
        System.out.println(feeList);
        if (list.size()>0) {
            for (String s : list) {
                String selectSql="SELECT * FROM ais_fee_plan WHERE start_date<'2019-01-01' and status<>'04' and id not in (SELECT fee_plan_id FROM ais_fee )";
                List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(selectSql);
                for (Map<String, Object> objectMap : queryForList) {
                    Object id = objectMap.get("id");
                    String delectSql="DELETE FROM ais_fee WHERE id='"+id+""+"'";
                    try {
                        jdbcTemplate.update(delectSql);
                    } catch (DataAccessException e) {
                        e.printStackTrace();
                    }
                    Map<String, Object> inSql = new HashMap<>();
                    inSql.put("id", UUIDGenerator.generate());
                    inSql.put("plan_code",objectMap.get("code"));
                    inSql.put("plan_fee",objectMap.get("fee"));
                    inSql.put("fee",objectMap.get("fee"));
                    inSql.put("fee_date",objectMap.get("start_date"));
                    inSql.put("customer_name",objectMap.get("customer_name"));
                    inSql.put("customer_id",objectMap.get("customer_id"));
                    inSql.put("fee_plan_id",objectMap.get("id"));
                    inSql.put("address",objectMap.get("address"));
                    inSql.put("fee_type",objectMap.get("recetype"));
                    inSql.put("user_name","test");
                    inSql.put("property","00");
                    inSql.put("state","0");
                    inSql.put("remark","自动补充收费");
                    inSql.put("approval","10");
                    inSql.put("sys_org_code",objectMap.get("sys_org_code"));
                    insert(feeList,inSql);
                    String sql="UPDATE ais_fee_plan set sumfee=fee ,status='02' WHERE id='"+id+"'";
                    jdbcTemplate.update(sql);
                }
            }
        }
        return null;
    }

    private void insert(List<Map<String, Object>> list,Map<String,Object> map){
        StringBuilder builder = new StringBuilder();
        StringBuilder bu = new StringBuilder();
        builder.append("INSERT INTO ais_fee (");
        bu.append(" VALUES (");
        if (list.size()>0) {
            for (int i = 0; i < list.size(); i++) {
                if (i!=list.size()-1){
                    String key = list.get(i).get("COLUMN_NAME").toString();
                    builder.append(" "+key+" , ");
                    Object values = map.get(key);
                    if (values!=null) {
                        bu.append("'"+values.toString()+"',");
                    }else {
                        bu.append("NULL ,");
                    }
                }else {
                    String key = list.get(i).get("COLUMN_NAME").toString();
                    builder.append(" "+key+" )");
                    Object values = map.get(key);
                    if (values!=null) {
                        bu.append("'"+values.toString()+"' )");
                    }else {
                        bu.append("NULL )");
                    }
                }

            }

        }
        System.out.println(builder.toString());
        System.out.println(bu.toString());
        String insertSql=builder.toString()+" "+bu.toString();
        jdbcTemplate.update(insertSql);
    }

    public static void main(String[] args) {
       /* com.spire.doc.Document document = new com.spire.doc.Document();
        document.loadFromFile("E:\\word\\test");
        BigDecimal bigDecimal = new BigDecimal("0");*/
        /*
         * //加载需要设置成水印的图片 PictureWatermark picture = new PictureWatermark();
         * picture.setPicture(imgPath);// 是图片路径 picture.setScaling(100);
         * picture.isWashout(false);
         *
         * //将图片设置成水印 document.setWatermark(picture);
         */
        /*TextWatermark txtWatermark = new TextWatermark();
        txtWatermark.setText("上海易辉房地产开发有限公司");
        txtWatermark.setFontSize(50);
        txtWatermark.setColor(Color.gray);
        txtWatermark.setLayout(WatermarkLayout.Diagonal);
        document.getSections().get(0).getDocument().setWatermark(txtWatermark);

        //保存文档
        document.saveToFile("E:\\word\\test123", FileFormat.Doc);*/
    }
}
